package handler

import (
	"context"
	"didi-vehicle-certification/srv/global"
	"didi-vehicle-certification/srv/internal/model"
	pb "didi-vehicle-certification/srv/proto"
	"fmt"
	"go.uber.org/zap"
)

type CartServer struct {
	pb.UnimplementedCartServer
}

func (c *CartServer) CartRegister(ctx context.Context, req *pb.CartRegisterRequest) (resp *pb.CartRegisterResponse, err error) {

	cart := model.Cart{
		CartUrl:    req.CartUrl,
		CartNumber: req.CartNumber,
		CartAlias:  req.CartAlias,
		CartType:   req.CartType,
		DriverID:   int(req.DriverId),
		TypeID:     int(req.TypeId),
	}
	err = global.DB.Create(&cart).Error
	if err != nil {
		return nil, fmt.Errorf("注册失败" + err.Error())
	}
	zap.S().Info("注册成功")
	global.Rdb.ZIncrBy("hot_type", 1, cart.CartType)
	return &pb.CartRegisterResponse{Message: "车辆注册成功"}, nil
}
func (c *CartServer) CartUpdate(ctx context.Context, req *pb.CartUpdateRequest) (resp *pb.CartUpdateResponse, err error) {

	var cart model.Cart
	err = global.DB.Where("id = ?", req.CartId).First(&cart).Error
	cart.CartUrl = req.CartUrl
	cart.CartAlias = req.CartAlias
	cart.CartType = req.CartType
	cart.CartNumber = req.CartNumber
	cart.CartUrl = req.CartUrl
	if err = global.DB.Save(&cart).Error; err != nil {
		return nil, fmt.Errorf("修改失败")
	}
	return &pb.CartUpdateResponse{Message: "车辆信息修改成功"}, nil
}
func (c *CartServer) CartDetail(ctx context.Context, req *pb.CartDetailRequest) (resp *pb.CartDetailResponse, err error) {

	var cart []model.Cart
	global.DB.Where("id = ?", req.CartId).Find(&cart)
	var slice []*pb.CartMsg
	for _, v := range cart {
		slice = append(slice, &pb.CartMsg{
			CartUrl:    v.CartUrl,
			CartNumber: v.CartNumber,
			CartAlias:  v.CartAlias,
			CartType:   v.CartType,
			DriverId:   int32(v.DriverID),
		})
	}
	return &pb.CartDetailResponse{Detail: slice}, nil
}
func (c *CartServer) CartDel(ctx context.Context, req *pb.CartDelRequest) (resp *pb.CartDelResponse, err error) {

	var cart model.Cart
	global.DB.Where("id = ?", req.CartId).Delete(&cart)
	return &pb.CartDelResponse{Message: "删除成功"}, nil
}

func (c *CartServer) CartClassFind(ctx context.Context, req *pb.CartClassFindRequest) (resp *pb.CartClassFindResponse, err error) {

	children, err := GetClassWithChildren(req.Pid)
	if err != nil {
		return nil, fmt.Errorf("展示失败")
	}
	var slice []*pb.Class
	for _, t := range children.Type {
		slice = append(slice, &pb.Class{
			ClassName: t.ClassName,
			Pid:       int32(t.Pid),
			Id:        int32(t.ID),
		})
	}
	return &pb.CartClassFindResponse{
		Class: slice,
	}, nil
}
func GetClassWithChildren(pid int32) (model.Type, error) {
	var parent model.Type
	global.DB.Where("id  = ?", pid).First(&parent)
	children := GetChildren(pid)
	parent.Type = children
	for i, t := range parent.Type {
		withChildren, err := GetClassWithChildren(int32(t.ID))
		if err != nil {
			continue
		}
		parent.Type[i] = withChildren
	}
	return parent, nil
}

func GetChildren(pid int32) []model.Type {
	var types []model.Type
	err := global.DB.Where("pid  = ?", pid).Find(&types).Error
	zap.S().Error(err)
	return types
}
func (c *CartServer) Rank(ctx context.Context, req *pb.RankRequest) (resp *pb.RankResponse, err error) {

	result, err := global.Rdb.ZRevRangeWithScores("hot_type", 0, 9).Result()
	if err != nil {
		return nil, err
	}
	var slice []*pb.Rank
	for _, z := range result {
		slice = append(slice, &pb.Rank{
			Count: int32(z.Score),
			Name:  z.Member.(string),
		})
	}
	return &pb.RankResponse{
		Rank: slice,
	}, nil
}
func (c *CartServer) Like(ctx context.Context, req *pb.LikeRequest) (resp *pb.LikeResponse, err error) {

	global.Rdb.IncrBy("search"+req.Name, 1)
	return &pb.LikeResponse{Msg: "搜索成功"}, nil
}

func (c *CartServer) FindCart(ctx context.Context, req *pb.FindCartRequest) (resp *pb.FindCartResponse, err error) {

	var cart []model.Cart
	query := global.DB.Model(&cart)
	if req.CartNumber != "" {
		query.Where("cart_number = ?", req.CartNumber)
	}
	if req.CartAlias != "" {
		query.Where("cart_alias = ?", req.CartAlias)
	}
	query.Find(&cart)
	var slice []*pb.CartMsg
	for _, m := range cart {
		slice = append(slice, &pb.CartMsg{
			CartUrl:    m.CartUrl,
			CartNumber: m.CartNumber,
			CartAlias:  m.CartAlias,
			CartType:   m.CartType,
			DriverId:   int32(m.DriverID),
		})
	}
	return &pb.FindCartResponse{Detail: slice}, nil
}
func (c *CartServer) AddClass(ctx context.Context, req *pb.AddClassRequest) (resp *pb.AddClassResponse, err error) {

	var Class model.Type
	if req.Pid != 0 {
		Class = model.Type{
			Pid:       int(req.Pid),
			ClassName: req.Name,
		}
	} else {
		Class.Pid = 0
	}
	global.DB.Create(&Class)

	return &pb.AddClassResponse{Msg: "分类添加成功"}, nil
}
